<template>
  <div class="login">
    <div class="login-box">
      <!-- 标题 -->
      <div class="title">后台管理系统</div>

      <!-- 登录表单 -->
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password>
          </el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item>
          <el-button type="primary" class="loginBtn" @click="loginFun">登录</el-button>
          <el-link type="info" class="loginLink" @click="$router.push('/register')">去注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/index.js'
import { mapMutations } from 'vuex'

export default {
  data () {
    return {
      // 登录表单数据
      loginForm: {
        username: 'xiaonan',
        password: 'xiaonan520'
      },
      // 登录表单验证规则
      loginRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10的大小写字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapMutations(['setToken']),
    loginFun () {
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) {
          return this.$message.error('请填写正确的登录信息')
        } else {
          const { data: res } = await loginAPI(this.loginForm)
          if (res.code !== 0) return this.$message.error(res.message)
          this.setToken(res.token)
          // 保存token到本地存储
          this.$message.success('登录成功')
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  height: 100vh;
  width: 100%;
  background-image: url(../../assets/backgroundImg.jpg);
  background-size: cover;
}

.login-box {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  width: 400px;
  height: 275px;
  background-color: #fff;

  .title {
    text-align: center;
    font-size: 28px;
    color: #007AFF;
    margin-top: 10px;
  }

  .loginBtn {
    width: 100%;
  }

  .loginLink {
    line-height: 20px !important;
    margin-top: 10px;
  }
}
</style>
